import * as vscode from 'vscode';
import {dedent} from "../../../utils";

// documentation中使用了代码块，代码块中写的事makedown格式，首行不能为空
let slItems = [
    {
        "label": "fd",
        "insertText": dedent`
        function \${1:functionName}(\${2:params})
            \${3:-- body}
            
        end
        `,
        "documentation": dedent`
        函数定义
        `
    },
    {
        "label": "ifelse",
        "insertText": dedent`
        if \${1:param} then
            \${2:-- body}
        
        else
            \${3:-- body}
        
        end

        `,
        "documentation": dedent`
        条件语句
        `
    },
    {
        "label": "elseif",
        "insertText": dedent`
        if \${1:params1} then
            \${3:-- body}

        elseif \${2:params2} then
            \${4:-- body}

        else
            \${5:-- body}

        end

        `,
        "documentation": dedent`
        条件语句
        `
    },
    {
        "label": "ifd",
        "insertText": dedent`
        if \${1:param} then
            \${2:-- body}

        end

        `,
        "documentation": dedent`
        条件语句
        `
    },
    {
        "label": "ford",
        "insertText": dedent`
        for i = \${1:pageStart}, \${2:pageEnd} do
            \${3:-- body}

        end

        `,
        "documentation": dedent`
        for 循环
        `
    },
    {
        "label": "whiled",
        "insertText": dedent`
        while \${1:param} do
            \${2:-- body}

        end

        `,
        "documentation": dedent`
        while 循环
        `
    },
    {
        "label": "fort",
        "insertText": dedent`
        for \${1:key}, \${2:value} in pairs(\${3:tablename}) do
            \${4:-- body}

        end

        `,
        "documentation": dedent`
        迭代表
        `
    },
    {
        "label": "md",
        "insertText": dedent`
        local \${1:Mclass} = {}

        function \${1:Mclass}.\${2:functionName}(\${3:param})
            \${4:-- body}

        end

        return \${1:Mclass}

        `,
        "documentation": dedent`
        模块（Module）
        `
    },
    {
        "label": "mtd",
        "insertText": dedent`
        local \${1:t} = {}
        local \${2:mt} = {
            __index = \${3:function}(\${4:table}, \${5:key})
                \${6:-- body}

            end
        }
        setmetatable(\${1:t}, \${2:mt})

        `,
        "documentation": dedent`
        元表（Metatable）
        `
    },
    {
        "label": "ed",
        "insertText": dedent`
        local status, err = pcall(\${1:functionName})
        if not status then
            print("Error: " .. err)
        end

        `,
        "documentation": dedent`
        错误处理
        `
    },
    {
        "label": "repeat",
        "insertText": dedent`
        repeat
            \${1:-- body}
            
        until \${2:condition}
        `,
        "documentation": dedent`
        repeat until 循环模板
        `
    },
    {
        "label": "local",
        "insertText": dedent`
        local \${1:var_name} = \${2:value}
        `,
        "documentation": dedent`
        repeat until 循环模板
        `
    },
]


export function getCompletionItems(): vscode.CompletionItem[] {


    const completionItems: vscode.CompletionItem[] = [];
    slItems.forEach((item: any) => {
        completionItems.push(createFunctionCompletionItem(item.label, item.insertText, item.documentation));
    });
    return completionItems;
}

function createFunctionCompletionItem(label: string, insertText: string, documentation?: string): vscode.CompletionItem {
    const item = new vscode.CompletionItem(label, vscode.CompletionItemKind.Function);
    item.insertText = new vscode.SnippetString(insertText);
    if (documentation) {
        item.documentation = new vscode.MarkdownString(documentation);
    }
    return item;
}
